package org.xiaov.lunasql.engine.transform.interval;

import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntervalExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntervalUnit;
import org.dromara.hutool.core.text.split.SplitUtil;

import java.util.List;

/**
 * <p>
 * DaySecond2SecondIntervalHandler 天秒转换为秒
 * </p>
 *
 * @author lzq
 * @since 2024/12/24 16:25
 */
public class DaySecond2SecondIntervalHandler implements IntervalHandler {
    @Override
    public void interval(SQLIntervalExpr x) {
        x.setUnit(SQLIntervalUnit.SECOND);
        if (x.getValue() instanceof SQLCharExpr charExpr) {
            List<String> daySecond = SplitUtil.splitByBlank(charExpr.getText());
            if (daySecond.size() == 2) {
                String day = daySecond.get(0);
                String second = daySecond.get(1);
                x.setValue(new SQLIntegerExpr(Integer.parseInt(day) * 24 * 60 * 60 + Integer.parseInt(second)));
            } else if (daySecond.size() == 1) {
                String second = daySecond.get(0);
                x.setValue(new SQLIntegerExpr(Integer.parseInt(second)));
            }
        }
    }
}
